<template>
  <contents :heightInclude="['tabBar']" class="switchLanguage_page">
    <u-radio-group v-model="value">
      <u-cell-group>
        <u-cell-item @click="value = item.code" v-for="item in langList" :title="item.languageName" :key="item.id" :arrow="false">
          <u-radio :name="item.code"> </u-radio>
        </u-cell-item>
      </u-cell-group>
    </u-radio-group>
    <SubmitBtn @click="submit" />
  </contents>
</template>
<script>
import { languageManagement } from "@/api/http/user";
import SubmitBtn from '@/components/submitBtn'
export default {
  components: {
    SubmitBtn
  },
  data() {
    return {
      value: this.$storage.get('language') || '',
      langList: []
    }
  },
  onLoad() {
    this.getData()
  },
  methods: {
    getData() {
      languageManagement({
        system: 'tomorni',
        // #ifdef H5
        terminal: 'h5',
        // #endif
        // #ifdef APP-PLUS
        terminal: 'app'
        // #endif
      }).then(res => {
        if (res.code === 200) {
          this.langList = res.data
        }
      })
    },

    submit() {
      const val = this.value || uni.getSystemInfoSync().language
      this._i18n.locale = val
      this.langList.map(item => {
        if (this.value === item.code) {
          this.$storage.set('languageName', item.languageName)
        } 
      })
      this.$storage.set('language', this.value)
      const tabLocales = this.$t('locales')
      uni.setTabBarItem({
        index: 0,
        text: tabLocales['pages/index/indexHome/index']
      })
      uni.setTabBarItem({
        index: 1,
        text: tabLocales['pages/sort/index']
      })
      uni.setTabBarItem({
        index: 2,
        text: tabLocales['pages/shoppingCart/index']
      })
      uni.setTabBarItem({
        index: 3,
        text: tabLocales['pages/me/index']
      })
      // #ifdef APP-PLUS
      plus.runtime.restart();
      // #endif
      // #ifdef H5
      window.location.href = window.location.origin
      // #endif
    }
  }
}
</script>
<style lang="scss">
.switchLanguage_page {
  .u-radio-group {
    width: 100%;
  }
}
</style>